import Vue from 'vue'
import router from "../router";
import store from "../store/";

const v = new Vue();
let error = {};
/**
 * 接口返回status处理
 */
const redirectTo = () => {
  if (error.response.status === 404) {
    // alert('接口找不到，找后端去！');
  }

  function clearSessionStorage(responseData) {
    v.$message.error(responseData);
    router.push('/login');
    sessionStorage.removeItem("token");
    sessionStorage.removeItem("refresh_token");
    sessionStorage.removeItem("username");
    sessionStorage.removeItem("userId");
    sessionStorage.removeItem("permissions");
    sessionStorage.removeItem("isTipPassUpdate");
    sessionStorage.removeItem("TENANT-ID");
    sessionStorage.removeItem("tabList");
    sessionStorage.removeItem("allUserInfo");
    sessionStorage.removeItem("roleCodeName");
    localStorage.clear();
  }

  if (error.response.status === 401) {
    let isLogTimeOver = sessionStorage.getItem("isLogTimeOver");
    let isUserOver = sessionStorage.getItem("isUserOver");
    let responseData = error.response.data.data;
    if (responseData === '登录超时，请重新登录' && 'false' === isLogTimeOver) {
      // 清除指定 token
      clearSessionStorage(responseData);
      sessionStorage.setItem("isLogTimeOver", true);
    } else if (responseData === '当前登录人已在另一客户端登录' && 'false' === isUserOver) {
      // 清除指定 token
      clearSessionStorage(responseData)
      sessionStorage.setItem("isUserOver", true);
    } else if ('true' != isLogTimeOver && 'true' != isUserOver) {
      clearSessionStorage(responseData)
    }
  }
};

export default {
  doError: (err) => {
    error = err || {};
    redirectTo();
  }
};
